﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
using System.ComponentModel;
using System.Data.Linq.Mapping;

namespace ch0p57ickz.OnTime
{
    [Table]
    public class Appointment : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int _Id, _ParentId;
        private string _Name, _Location;
        private DateTime _Time;
        private bool _Recurring, _Skip;
        private TransportMethod _Method;

        [Column(IsPrimaryKey=true, IsDbGenerated=true, DbType="INT NOT NULL Identity", CanBeNull= false, AutoSync = AutoSync.OnInsert)]
        public int ID
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    NotifyPropertyChanging("AppointmentId");
                    _Id = value;
                    NotifyPropertyChanged("AppointmentId");
                }
            }
        }

        [Column]
        public int ParentID
        {
            get
            {
                return _ParentId;
            }
            set
            {
                if (_ParentId != value)
                {
                    NotifyPropertyChanging("ParentID");
                    _ParentId = value;
                    NotifyPropertyChanged("ParentID");
                }
            }
        }

        [Column]
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (_Name != value)
                {
                    NotifyPropertyChanging("Name");
                    _Name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        [Column]
        public string Location
        {
            get
            {
                return _Location;
            }
            set
            {
                if (_Location != value)
                {
                    NotifyPropertyChanging("Location");
                    _Location = value;
                    NotifyPropertyChanging("Location");
                }
            }
        }

        [Column]
        public DateTime NextAppointmentTime
        {
            get
            {
                return _Time;
            }
            set
            {
                if (_Time != value)
                {
                    NotifyPropertyChanging("NextAppointmentTime");
                    _Time = value;
                    NotifyPropertyChanged("NextAppointmentTime");
                }
            }
        }

        [Column]
        public bool Recurring
        {
            get
            {
                return _Recurring;
            }
            set
            {
                if (_Recurring != value)
                {
                    NotifyPropertyChanging("Recurring");
                    _Recurring = value;
                    NotifyPropertyChanged("Recurring");
                }
            }
        }

        [Column]
        public bool Skip
        {
            get
            {
                return _Skip;
            }
            set
            {
                if (_Skip != value)
                {
                    NotifyPropertyChanging("Skip");
                    _Skip = value;
                    NotifyPropertyChanged("Skip");
                }
            }
        }

        [Column]
        public TransportMethod Method
        {
            get
            {
                return _Method;
            }
            set
            {
                if (_Method != value)
                {
                    NotifyPropertyChanging("Method");
                    _Method = value;
                    NotifyPropertyChanged("Method");
                }
            }
        }

        public String Description
        {
            get
            {
                return String.Format(
                "{0} {1} {2}",
                Location,
                (Recurring) ? NextAppointmentTime.ToShortTimeString() : NextAppointmentTime.ToString());
            }
        }


        #region Changing Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
        #endregion
    }

    public enum TransportMethod
    {
        Walking,
        Driving,
        Bus,
        Mixed
    }

    public class AppointmentsDataContext : DataContext
    {
        public static string DBConnectionString = "Data source=isostore:/appointments.sdf";
        private static AppointmentsDataContext _dataContext = null;

        private AppointmentsDataContext(string connectionString)
            : base(connectionString)
        {
            if (!this.DatabaseExists())
                CreateDatabase();
        }

        public void AddAppointment(String name, String location, DateTime appointmentTime, bool recurring)
        {
            AppointmentItems.InsertOnSubmit(new Appointment() { Name = name, Location = location, NextAppointmentTime = appointmentTime, Recurring = recurring });
        }

        public Table<Appointment> AppointmentItems;

        public static AppointmentsDataContext Instance
        {
            get
            {
                if (_dataContext == null)
                    _dataContext = new AppointmentsDataContext(AppointmentsDataContext.DBConnectionString);
                return _dataContext;
            }
        }
        
    }
}
